using MoonFramework.Scripts.Event;
using MoonFramework.Template;
using Serilog;
using UnityEngine;
using UnityEngine.UI;

namespace MoonFramework.Scripts.View.UI
{
	public class LoadUIWindow : BaseUIWindow
	{
		[SerializeField]
		private Text progress_Text;
		[SerializeField]
		private Image fill_Image;
		
		public override void Show()
		{
			base.Show();
			UpdateProgress(0);
		}

		protected override void SendEvent()
		{
			base.SendEvent();
			EventCenter.Instance.SendCommand<float>("LoadingSceneProgress", UpdateProgress);
			EventCenter.Instance.SendCommand("LoadSceneSucceed", OnLoadSceneSucceed);
		}

		protected override void CancelEventListener()
		{
			base.CancelEventListener();
			EventCenter.Instance.RemoveCommand<float>("LoadingSceneProgress", UpdateProgress);
			EventCenter.Instance.RemoveCommand("LoadSceneSucceed", OnLoadSceneSucceed);
		}

		/// <summary>
		/// 当场景加载成功
		/// </summary>
		private void OnLoadSceneSucceed()
		{
			Close();
		}

		/// <summary>
		/// 更新进度
		/// </summary>
		private void UpdateProgress(float progressValue)
		{
			Log.Debug((int)(progressValue * 100) + "%");
			progress_Text.text = (int)(progressValue * 100) + "%";
			fill_Image.fillAmount = 0;
		}
	}
}
